package com.Luooh.weather;

import java.util.HashMap;
import java.util.List;

import com.Luooh.adapter.LifeAdapter;
import com.Luooh.bean.History_ForeCast;
import com.Luooh.bean.WeatherInfoRetail;
import com.Luooh.bean.weather.WeatherResponceArgs;
import com.Luooh.utils.Constant;
import com.Luooh.utils.HttpUtils;
import com.Luooh.utils.JsonParseUtils;
import com.Luooh.utils.JsonUtils;
import com.Luooh.utils.ListViewUtils;
import com.Luooh.utils.NetConnectUtil;
import com.Luooh.utils.WeatherApplication;
import com.Luooh.utils.NetConnectUtil.Status;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements NetConnectUtil.Listener
{
	private ListView lv_life;
	private ListView lv_feature_weather;
	private LifeAdapter adapter;
	private Handler handler1 = new Handler(){
	    public void handleMessage(Message msg) {
		int status = msg.arg1;
		if(status == Status.COMPLETE){
		    String url = (String) msg.obj;
		    NetConnectUtil netConnectUtil = new NetConnectUtil(MainActivity.this, handler, url, MainActivity.this);
		    netConnectUtil.connect();
		}
	    };
	};
	
	private Handler handler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			String result = (String) msg.obj;
			Log.i("LOH", result);
			Toast.makeText(MainActivity.this, result, 1).show();
			WeatherInfoRetail fromJsonToObject = (WeatherInfoRetail) JsonUtils.fromJsonToObject(result, WeatherInfoRetail.class);
//			JsonParseUtils.parseStatus(result);
			//adapter = new LifeAdapter(MainActivity.this, WeatherApplication.getInstance().getLifeinfo().getInfoMap());
			lv_life.setAdapter(adapter);
			ListViewUtils.setListViewHeightBasedOnChild(lv_life);
		};
	};
	
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.fragment_main);
		
		initView();
		initData();
		//prepData();
	}

	private void initData() {
	    NetConnectUtil netConnectUtil = new NetConnectUtil(MainActivity.this, handler1, Constant.reqHost + Constant.token, this);
	    netConnectUtil.connect();
	}

	private void prepData()
	{
		new Thread()
		{
			public void run() 
			{
				try {
					Message msg = handler.obtainMessage();
					String result = HttpUtils.get(Constant.reqHost + Constant.token);
					msg.obj = result;
					handler.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
				}
			};
		}.start();
	}
	
	private void initView()
	{
		lv_life = (ListView) findViewById(R.id.lv_life);
		lv_feature_weather = (ListView) findViewById(R.id.lv_feature_weather);
	}

	@Override
	public void status(NetConnectUtil netConnectUtil, int status) {
	    
	}
}
